---
title: "Regression analysis - Japanese"
author: "`r Sys.info()['user']`"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)
require(memisc)
source("functions.R")
source("models.R")
dat <- cbind(readRDS("data_lss_ja.RDS"), 
             readRDS("data_newsmap_ja.RDS"), 
             readRDS("data_dictionary_ja.RDS"))
dat <- subset(dat, class %in% c("kp", "jp") & date <= as.Date("2018-12-31"))
events <- yaml::read_yaml("events.yml")

window <- 60
dat <- flag_events(dat, events$JP, window)
dat2 <- flag_events(dat, events$JP, window, merge = FALSE)
```

## Variables 

- publication: Asahi or Yomiuri
- class: article's focus to Japan or North Korea
- keywords (see dictionary_ja.yml):
    - government: mention of prime minister or his cabinet members
- evens (see events.yml)
    - election: within `r window` days before elections
    - legislation: within `r window` days before security legislation
    - military: within `r window` days after major military events
    - diplomacy: within `r window` days after major diplomatic events

## Models

```{r results="asis"}
mtable("Model 0" = lm(f0, dat), 
       "Model 1" = lm(f1, dat),
       "Model 2" = lm(f2, dat), 
       "Model 3" = lm(f3, dat), 
       "Model 4" = lm(f4, dat),
       "Model 5" = lm(f5, dat),
       show.baselevel = FALSE,
       summary.stats = c("sigma", "R-squared", "F", "N")) %>% 
    relabel("TRUE" = "", "publication: " = "", "class: " = "", 
            "military(: )?" = "military (M)", 
            "diplomacy(: )?" = "diplomacy (D)", 
            "election(: )?" = "election (E)", 
            "legislation(: )?" = "legislation (L)", 
            gsub = TRUE) %>%
    show_html()
```

```{r results="asis"}
mtable("Model 2" = lm(f2, dat2), 
       "Model 6" = lm(f3, dat2), 
       "Model 7" = lm(f5, dat2),
       show.baselevel = FALSE,
       coef.style = "horizontal",
       summary.stats = c("sigma", "R-squared", "p", "N")) %>% 
    relabel("TRUE" = "", "publication: " = "", "class: " = "", 
            "military(: )?(\\d+)" = "military (M\\2)", 
            "diplomacy(: )?(\\d+)" = "diplomacy (D\\2)", 
            "election(: )?(\\d+)?" = "election (E\\2)", 
            "legislation(: )?(\\d+)?" = "legislation (L\\2)", 
            gsub = TRUE) %>%
    show_html()
```

```{r results="asis"}
dat3 <- subset(dat, class == "kp")
mtable("Model 8" = glm(f7, dat3, family = binomial), 
       "Model 9" = glm(f8, dat3, family = binomial),
       show.baselevel = FALSE,
       getSummary = getSummary_expcoef,
       coef.style = "horizontal",
       summary.stats = c("Log-likelihood", "AIC", "N")) %>% 
    relabel("TRUE" = "", "publication: " = "", "class: " = "", 
            "military(: )?" = "military (M)", 
            "diplomacy(: )?" = "diplomacy (D)", 
            "election(: )?" = "election (E)", 
            "legislation(: )?" = "legislation (L)", 
            gsub = TRUE) %>%
    show_html()
```

```{r}
t.test(lss ~ alone, dat3)
```

## Predicted values

```{r fig.height=5, fig.width=10}
dat_dummy <- expand.grid(
    publication = unique(dat2$publication),
    class = c("kp", "jp"), government = FALSE, 
    alone = c(TRUE, FALSE),
    election = factor(0),
    legislation = factor(1:4),
    military = factor(0),
    diplomacy = factor(0)
)
pred <- predict(lm(f5, dat2), newdata = dat_dummy, interval = "confidence", level = 0.9)
dat_dummy <- cbind(dat_dummy, pred)
par(mfrow = c(1, 2), cex = 1.0)
ylim <- c(-0.3, 1.3)

plot_prediction(subset(dat_dummy, publication == "asahi" & class == "kp" & !alone), col = color[2], 
                main = "North Korea", xlim = c(0.7, 4.3), ylim = ylim)
plot_prediction(subset(dat_dummy, publication == "yomiuri" & class == "kp" & !alone), col = color[1], 
                add = TRUE)
legend("topleft", c("Asahi", "Yomiuri"), col = color[2:1], lty = 1, lwd = 2, horiz = TRUE)
plot_prediction(subset(dat_dummy, publication == "asahi" & class == "kp" & alone), col = color[2], 
                main = "Only North Korea", xlim = c(0.7, 4.3), ylim = ylim)
plot_prediction(subset(dat_dummy, publication == "yomiuri" & class == "kp" & alone), col = color[1], 
                add = TRUE)
legend("topleft", c("Asahi", "Yomiuri"), col = color[2:1], lty = 1, lwd = 2, horiz = TRUE)
```


## Multi-colinear

```{r}
cor(dat[,c("election", "legislation", "military", "diplomacy")])
```

```{r fig.height=2, fig.width=10}
plot_dummy(events$JP, window, main = "Japanese")
```

